# $Id: GNUmakefile,v 1.4 2011/01/15 06:42:36 pgurumur Exp $
# Copyright (c) 2009 Prabhu Gurumurthy  <pgurumur@gmail.com>
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

# version "$Revision: 1.4 $"
# author "$Author: pgurumur $"

PROG := egpd
UNAME	:= $(shell uname)
CURDIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
CC := gcc
CFLAGS	+= -O2 -std=gnu99 -W -Wall -Werror -Wmissing-prototypes
CFLAGS	+= -Wmissing-declarations -Wshadow -Wpointer-arith -Wcast-qual 
CFLAGS	+= -Wsign-compare -Wstrict-prototypes -Wa,--fatal-warnings
CFLAGS	+= -Wmissing-include-dirs -Wunused -Wuninitialized -Wstack-protector
CFLAGS	+= -fPIC -pie -fstack-protector-all -I. -I${CURDIR}
BFLAGS	:= -fPIC
LDFLAGS	:=

ifeq ($(UNAME), $(filter $(UNAME), OpenBSD NetBSD))
YACC := yacc
CFLAGS += -Wno-traditional -Wno-pointer-sign -pie
endif

ifeq ($(UNAME), Linux)
YACC	:= bison -y
CFLAGS += -D_XOPEN_SOURCE -D_BSD_SOURCE
endif

ifeq ($(UNAME), Darwin)
YACC := bison -y
CFLAGS += -I/opt/local/include
LDFLAGS += -L/opt/local/lib
endif

ifeq ($(CC), icc)
CFLAGS	+= -fPIE -axSSE4.2 -march=core-avx-i 
BFLAGS	+= -fPIE
endif

YSRC	:= $(patsubst %.y, %.c, $(wildcard *.y))
YINC	:= $(patsubst %.y, %.h, $(wildcard *.y))
OBJS	:= $(patsubst %.c, %.o, $(wildcard *.c))
RM := /bin/rm -f

all : $(PROG) $(YSRC)

$(PROG): $(YSRC) $(OBJS) 
	@echo "building $@, using $(CC)"
	@${CC} $(BFLAGS) -o $@ $^ ${LDFLAGS}

%.c: %.y
	@echo "compiling $<"
	@${YACC} -d -o $ $@ $<
	@$(CC) $(CFLAGS) -c $@

%.o: %.c
	@echo "compiling $<"
	@${CC} ${CFLAGS} -c -o $@ $<

clean:
	@echo "cleaning... *.core ${PROG} ${OBJS} $(YSRC) $(YINC)"
	@${RM} *.core ${PROG} ${OBJS} $(YSRC) $(YINC)
